#pragma once

#include "highfive/H5File.hpp"

#include "DBManager/IRepository/Input/Coordinate/ITRANSFORMATIONRepository.h"
#include "DBManager/Repository/HDF5/Base/MainlyRepositoryBase.h"
SG_DBMANAGER_HDF5_NAMESPACE_OPEN
namespace TRANSFORMATION
{
    struct IDENTITY
    {
        SG::DataStructure::Common::Id                    CID = 0;         ///< Coordinate system identification number
        SG::DataStructure::FEM::TRANSFORMATIONData::Type TYPE =
            SG::DataStructure::FEM::TRANSFORMATIONData::Type::Undefined;  ///< Type of system
        SG::DataStructure::Common::I64 IINDEX    = 0;                     ///< Index into IDATA
        SG::DataStructure::Common::I64 RINDEX    = 0;                     ///< Index into RDATA
        SG::DataStructure::Common::Id  DOMAIN_ID = 0;                     ///<
    };
};  // namespace TRANSFORMATION
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DBManager::HDF5::TRANSFORMATION::IDENTITY> ()
{
    HighFive::CompoundType compoundType = { { "CID", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                            { "TYPE", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "IINDEX", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "RINDEX", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DOMAIN_ID", HighFive::create_datatype<SG::DataStructure::Common::Id> () } };
    return compoundType;
}

template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::TRANSFORMATIONData::IDATAData> ()
{
    HighFive::CompoundType ct = { { "DATA", HighFive::create_datatype<SG::DataStructure::Common::I64> () } };
    return ct;
}
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::TRANSFORMATIONData::RDATAData> ()
{
    HighFive::CompoundType ct = { { "DATA", HighFive::create_datatype<SG::DataStructure::Common::Real> () } };
    return ct;
}
SG_DBMANAGER_HDF5_NAMESPACE_OPEN
class TRANSFORMATIONRepository : public ITRANSFORMATIONRepository,
                                 public SG::DBManager::HDF5::MainlyRepositoryBase<SG::DataStructure::FEM::TRANSFORMATIONData>
{
    using MainStruct     = SG::DataStructure::FEM::TRANSFORMATIONData;
    using SubStructIDATA = SG::DataStructure::FEM::TRANSFORMATIONData::IDATAData;
    using SubStructRDATA = SG::DataStructure::FEM::TRANSFORMATIONData::RDATAData;

    using NasMainStruct = SG::DBManager::HDF5::TRANSFORMATION::IDENTITY;

  public:
    explicit TRANSFORMATIONRepository (HDF5Database& database);

    enum DATASET : std::uint8_t
    {
        MainDataset,
        IndexDataset,
        SubDataset_IDATA,
        SubDataset_RDATA
    };

    /**
     * @brief   : 初始化表数据
     * @author  : wuxingchuang
     */
    void InitialDatasetInfo () override;

    /**
     * @brief   : 将缓存数据转储到文件内
     */
    void DumpWriteCahce () override;

    /**
     * @brief   : 加载文件数据至缓存
     * @param   : loadIndex
     */
    std::vector<DataType>* LoadReadCache (std::size_t loadIndex) override;

    /**
     * @brief   : 对数据进行排序
     */
    void SortData () override;

    /**
     * @brief   : 获取所有数据
     */
    std::vector<MainStruct> GetAllData () override;

    /**
     * @brief   : 将自己的数据转存为nastran的数据结构
     */
    void OwnToNastran (std::vector<MainStruct>&          own,
                       std::vector<NasMainStruct>& _OUT  IDENTITYData,
                       std::vector<SubStructIDATA>& _OUT subDataIDATA,
                       std::vector<SubStructRDATA>& _OUT subDataRDATA,

                       bool isSort = false);

    /**
     * @brief   : 将NASTRAN的数据转存为自己的数据结构
     * @param   : IDENTITYData
     * @param   : subData
     * @param   : own
     */
    void NastranToOwn (std::vector<NasMainStruct>&   IDENTITYData,
                       std::vector<SubStructIDATA>&  subDataIDATA,
                       std::vector<SubStructRDATA>&  subDataRDATA,
                       std::vector<MainStruct>& _OUT own);
};
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE
